package com.gaussian.property.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaussian.property.app.vo.IcsUser;
import com.gaussian.property.module.biz.entity.*;
import com.gaussian.property.module.biz.service.IAppUserRoleService;
import com.gaussian.property.module.biz.service.IOptRoleService;
import com.gaussian.property.module.biz.service.IProjectService;
import com.gaussian.property.module.biz.service.IUserProjectService;
import com.songqi.common.core.constant.Constants;
import lombok.Data;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/5/17 21:00
 */
@Data
public class SynchronizeUserInfoThread implements Runnable {

    private AppUser appUser;

    private IcsUser icsUser;

    private IUserProjectService iUserProjectService;

    private IAppUserRoleService iAppUserRoleService;

    private IOptRoleService iOptRoleService;

    private IProjectService iProjectService;


    @Override
    public void run() {
        synchronizedProject(appUser, icsUser.getItems());
        refreshUserProject(appUser, icsUser.getItems());
        refreshUserRole(appUser, icsUser.getGaussian(), icsUser.getType());
    }


    /**
     * 刷新用户的角色关系
     * @param appUser
     * @param gaussian
     * @param type
     */
    private void refreshUserRole(AppUser appUser, String gaussian, String type) {
        LambdaQueryWrapper<AppUserRole> queryWrapper = new LambdaQueryWrapper<AppUserRole>()
                .eq(AppUserRole::getAppUserId, appUser.getAppUserId());
        iAppUserRoleService.remove(queryWrapper);
        if(Constants.YES_CHAR.equalsIgnoreCase(gaussian)){
            AppUserRole appUserRole = new AppUserRole();
            appUserRole.setAppUserId(appUser.getAppUserId());
            appUserRole.setOptRoleId("1");
            iAppUserRoleService.save(appUserRole);
        }else {
            LambdaQueryWrapper<OptRole> roleQueryWrapper = new LambdaQueryWrapper<OptRole>()
                    .eq(OptRole::getCode, type);
            OptRole optRole = iOptRoleService.getOne(roleQueryWrapper);
            if(optRole != null){
                AppUserRole appUserRole = new AppUserRole();
                appUserRole.setAppUserId(appUser.getAppUserId());
                appUserRole.setOptRoleId(optRole.getOptRoleId());
                iAppUserRoleService.save(appUserRole);
            }
        }
    }

    /**
     * 同步项目信息
     * @param items
     */
    private void synchronizedProject(AppUser appUser, List<IcsUser.Project> items){
        if(CollectionUtil.isNotEmpty(items)) {
            for (IcsUser.Project item : items) {
                Project project = iProjectService.getById(item.getItemId());
                if (project == null) {
                    project = new Project();
                    project.setProjectId(item.getItemId());
                    project.setProjectName(item.getItemName());
                    project.setCreateBy(appUser.getAppUserId());
                    project.setCreateTime(new Date());
                } else {
                    project.setProjectName(item.getItemName());
                    project.setUpdateBy(appUser.getAppUserId());
                    project.setUpdateTime(new Date());
                }
                iProjectService.saveOrUpdate(project);
            }
        }
    }


    private void refreshUserProject(AppUser appUser, List<IcsUser.Project> items) {
        LambdaQueryWrapper<UserProject> queryWrapper = new LambdaQueryWrapper<UserProject>()
                .eq(UserProject::getAppUserId, appUser.getAppUserId());
        iUserProjectService.remove(queryWrapper);
        if(CollectionUtil.isNotEmpty(items)){
            List<UserProject> userProjectList = new ArrayList<>();
            for(IcsUser.Project project : items){
                UserProject userProject = new UserProject();
                userProject.setAppUserId(appUser.getAppUserId());
                userProject.setProjectId(project.getItemId());
                userProjectList.add(userProject);

            }
            iUserProjectService.saveBatch(userProjectList);
        }

    }
}


